/*
* doubleyong create 2019/5/27
* 作用：用户相关状态存储
* */
import user from './../api/user'
import Vue from 'vue';
// import Vuex from 'vuex';

//状态即是数据
var state = {
  //登录加载框
  isLoading: false,
  //判断是否登录
  isLogin: Boolean(window.sessionStorage.getItem("isLogin")) ? Boolean(window.sessionStorage.getItem("isLogin")) : false,
  //token
  token: window.sessionStorage.getItem("token") || "",
  //用户菜单
  menuInfo: (window.sessionStorage.getItem("menuInfo") == "" ||
    window.sessionStorage.getItem("menuInfo") == "undefined") ?
    [] : JSON.parse(window.sessionStorage.getItem("menuInfo")),
  //用户信息
  userInfo: (window.sessionStorage.getItem("userInfo") == "" ||
    window.sessionStorage.getItem("userInfo") == "undefined"
  ) ?
    [] : JSON.parse(window.sessionStorage.getItem("userInfo")),
};


//处理状态的改变
var mutations = {
  SetToken(state, token) {  //设置token
    console.log(token);
    state.token = token;
    window.sessionStorage.setItem("token", token);
  },
  LoginSuccess(state, data) { //登录成功
    state.isLogin = true;
    state.menuInfo = data.menuInfo;
    state.userInfo = data.userInfo;
    window.sessionStorage.setItem("isLogin", true);
    window.sessionStorage.setItem("menuInfo", JSON.stringify(data.menuInfo));
    window.sessionStorage.setItem("userInfo", JSON.stringify(data.userInfo));
  },
  loginEdit(state) {  //注销
    state.isLogin = false;
    state.token = "";
    state.roleData = [];
    state.userInfo = [];
    window.sessionStorage.removeItem("isLogin");
    window.sessionStorage.removeItem("roleData");
    window.sessionStorage.removeItem("userInfo");
    window.sessionStorage.removeItem("token");
    console.log("退出成功");
  },
  LoadingStart(state) {
    state.isLoading = true;
  },
  LoadingEnd(state) {
    state.isLoading = false;
  },
  ClickActive(state, id) {
    // console.log(state.menuInfo.header);
    for (var i = 0; i < state.menuInfo.header.length; i++) {
      Vue.set(state.menuInfo.header[i], 'active', false)
    }
    Vue.set(state.menuInfo.header[id], 'active', true)
  },
  ClearActive(state) {
    for (var i = 0; i < state.menuInfo.header.length; i++) {
      Vue.set(state.menuInfo.header[i], 'active', false)
    }
  }
};

export default {
  state,
  mutations
}


